package com.example.basic.config;

import jdk.nashorn.internal.runtime.regexp.joni.Config;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * 数据相关配置
 * <p>
 * 1. MyBatis
 * 2. ElasticSearch  剔除
 * 3. MongoDB
 *
 * @author tds
 */
@Configuration
@AutoConfigureAfter(Config.class)
@MapperScan("com.example.**.mapper")
public class DataConfig implements InitializingBean {
    private final static Logger logger = LoggerFactory.getLogger(DataConfig.class);

    static {
        System.setProperty("es.set.netty.runtime.available.processors", "false");
    }

    @Autowired
    DataSource dataSource;
//
//    @Bean
//    public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean() {
//        try {
//            MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
//            sqlSessionFactoryBean.setDataSource(dataSource);
//            sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
//            sqlSessionFactoryBean.setGlobalConfig(mybatisPlusGlobalConfiguration());
//            sqlSessionFactoryBean.setPlugins(getMyBatisPlugins());
//            return sqlSessionFactoryBean;
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//    private Interceptor[] getMyBatisPlugins() {
//        // 分页插件
//        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
//        paginationInterceptor.setDialectType(DBType.MYSQL.getDb());
//        return new Interceptor[]{paginationInterceptor};
//    }
//
//    public GlobalConfiguration mybatisPlusGlobalConfiguration() {
//        GlobalConfiguration configuration = new GlobalConfiguration();
//        configuration.setSqlInjector(new AutoSqlInjector());
//        configuration.setDbType(DBType.MYSQL.getDb());
//        configuration.setIdType(IdType.ID_WORKER.getKey());
//        configuration.setDbColumnUnderline(true);
//        return configuration;
//    }
//
//    /**
//     * 启动xml mapper文件热加载, 只在开发环境启用
//     */
//    @Profile("default")
//    @Bean
//    public MybatisMapperRefresh mybatisMapperRefresh() throws Exception {
//        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
//        Resource[] resources = resourcePatternResolver.getResources("classpath*:com/example/**/learn/*.xml");
//        SqlSessionFactory sqlSessionFactory = mybatisSqlSessionFactoryBean().getObject();
//        return new MybatisMapperRefresh(resources, sqlSessionFactory, 10, 20, true);
//    }

    @Override
    public void afterPropertiesSet() throws Exception {

    }
}
